using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace StupidSpaceThing
{
	internal static class GuideSystem
	{

		private static Guide currentGuide = null;
		private static Queue<Guide> guides = new Queue<Guide>();
		private static SpriteBatch spriteBatch;
		private static Guide priorityGuide = null;
		private static bool priorityForce = false;
		public static bool PriorityForce { get { return priorityForce; } }
		private static Guide permanentGuide = null;
		public static Guide PermanentGuide { get { return permanentGuide; } }

		public static void Init(GraphicsDevice device) {
			spriteBatch = new SpriteBatch(device);
		}

		public static void Reset() {
			currentGuide = null;
			priorityGuide = null;
			priorityForce = false;
			permanentGuide = null;
			guides.Clear();
		}

		public static void Clear() {
			currentGuide = null;			
			guides.Clear();
		}

		public static void Enqueue(Guide guide) {
			guides.Enqueue(guide);
		}

		public static void Enqueue(Guide guide, bool clear) {
			if( clear ) {
				guides.Clear();
			}
			guides.Enqueue(guide);
		}

		public static void ShowPriority(Guide guide) {
			ShowPriority(guide, false);
		}
		public static void ShowPriority(Guide guide, bool force) {
			if( priorityForce ) return;
			priorityGuide = guide;
			priorityForce = force;
		}

		public static void ClosePriority() {
			if( priorityGuide != null ) {
				priorityGuide.End();
			}
		}
				
		public static void ShowPermanent(Guide guide) {
			permanentGuide = guide;
		}

		public static void ClosePermanent() {
			if( permanentGuide != null ) {
				permanentGuide.End();
			}
		}

		public static void Update(RealGameTime gameTime) {
			if( priorityGuide != null ) {
				priorityGuide.Update(gameTime);
			}
			if( permanentGuide != null ) {
				if( permanentGuide.StartTime == 0 ) permanentGuide.Start(gameTime.TotalRealGameTime.TotalMilliseconds);
				permanentGuide.Update(gameTime);
			}
			if( currentGuide != null ) {
				currentGuide.Update(gameTime);
			}
		}

		public static void Draw(RealGameTime gameTime) {
			if( priorityGuide != null ) {
				if( priorityGuide.Alpha == 0 && priorityGuide.State == GuideState.FadeOut ) {
					if( priorityGuide.Callback != null ) { priorityGuide.Callback(); }
					priorityGuide = null;
				} else {
					if( priorityGuide.StartTime == 0 ) priorityGuide.Start(gameTime.TotalRealGameTime.TotalMilliseconds);
					if( priorityGuide.Duration > 0 && priorityGuide.Duration + priorityGuide.StartTime < gameTime.TotalRealGameTime.TotalMilliseconds ) {
						priorityGuide.End();
					}					
					spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
					priorityGuide.Draw(spriteBatch);
					spriteBatch.End();
					return;
				}
			}
			if( currentGuide != null ) {
				if( currentGuide.Duration + currentGuide.StartTime < gameTime.TotalRealGameTime.TotalMilliseconds ) {
					currentGuide.End();
				}
				if( currentGuide.Alpha == 0 && currentGuide.State == GuideState.FadeOut ) {
					if( currentGuide.Callback != null ) { currentGuide.Callback(); }
					currentGuide = null;
				}
			}
			if( currentGuide == null && guides.Count > 0 ) {
				currentGuide = guides.Dequeue();
				currentGuide.Start(gameTime.TotalRealGameTime.TotalMilliseconds);
			}
			if( currentGuide != null ) {
				spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
				currentGuide.Draw(spriteBatch);
				spriteBatch.End();
			} else if( permanentGuide != null ){				
				spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
				permanentGuide.Draw(spriteBatch);
				spriteBatch.End();
			}
		}
	}
}
